package edu.hawaii.halealohacli.jcev;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;

/**
 * Tests the functionality of the functions in the "main" Control class.
 * @author Christopher Foo
 *
 */
public class TestControl {
  
  /**
   * The Control object to perform the tests on.
   */
  Control control = new Control();
  
  /**
   * Tests that the getClient method returns a non-null object when given a valid url and returns
   * null when given an invald url.
   */
  @Test
  public void testGetClient() {
    
    //Test valid server url
    WattDepotClient client = control.getClient("http://server.wattdepot.org:8190/wattdepot/");
    assertNotNull("Valid server creates non-null client object", client);
    
    //Test invalid server url
    WattDepotClient fakeclient = control.getClient("http://server.wattdepot.org:8190/fake/");
    assertNull("Invalid server creates null client object", fakeclient);
  }

  /**
   * Tests that the getCommandName method returns the command name as expected.
   */
  @Test
  public void testGetCommandName() {
    
    //Test command with arguments
    String commandName1 = control.getCommandName("command blah blah blah");
    assertEquals("Gets command name when arguments are included", "command", commandName1);
    
    //Test command with dashes and arguments
    String commandName2 = control.getCommandName("command-with-dashes blah");
    assertEquals("Gets command name with dashes when arguments are included", 
        "command-with-dashes",
        commandName2);

    //Test command without arguments
    String commandName3 = control.getCommandName("no-args");
    assertEquals("Gets command name when no arguments are included", "no-args", commandName3);
    
    //Test empty string.
    String commandName4 = control.getCommandName("");      
    assertEquals("Gets command name when empty string", "", commandName4);
  }
  
  /**
   * Tests that the getArgs method returns the arguments as expected.
   */
  @Test
  public void testGetArgs() {
    
    //Test command with multiple arguments
    String args1 = control.getArgs("command arg1 arg2 arg3");
    assertEquals("Gets all of the arguments", "arg1 arg2 arg3", args1);
    
    //Test command with one argument
    String args2 = control.getArgs("command argument");
    assertEquals("Gets argument when only one argument", "argument", args2);

    //Test command without arguments
    String args3 = control.getArgs("command");
    assertEquals("Gets empty string as arguments when no arguments are included", "", args3);
    
    //Test empty string.
    String args4 = control.getArgs("");      
    assertEquals("Gets empty string as arguments when empty string", "", args4);
  }
}
